<!DOCTYPE html>
<html lang="en">

   <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <style>
          * {
              margin: 0;
              padding: 0;
          }

          h2 {
              text-align: center;
          }

          .box {
              width: 600px;
              margin: 50px auto;
              display: flex;
              font-size: 25px;
              line-height: 40px;
          }

          .qs {

              width: 450px;
              height: 40px;
              color: red;

          }

          .btns {
              text-align: center;
          }

          .btns button {
              width: 120px;
              height: 35px;
              margin: 0 50px;
          }
      </style>
   </head>

   <body>
      <h2>随机点名</h2>
      <div class="box">
         <span>名字是：</span>
         <div class="qs">这里显示姓名</div>
      </div>
      <div class="btns">
         <button class="start">开始</button>
         <button class="end">结束</button>
      </div>

      <script>
         // 数据数组
         const arr = ['马超', '黄忠', '赵云', '关羽', '张飞']

         const name = document.querySelector('.qs')
         const start = document.querySelector('.start')
         const end = document.querySelector('.end')
         let timeID = 0
         let index = 0
         /*start.addEventListener('click', function () {
            timeID = setInterval(function () {
               index = Math.floor(Math.random() * arr.length)
               name.innerHTML = arr[index]
            }, 50)
            if (arr.length === 1) {
               start.disabled = true
               end.disabled = true
            }
         })

         end.addEventListener('click', function () {
            clearInterval(timeID)
            arr.splice(index, 1)
            console.log(arr)
         })*/
         start.onclick = function () {
            timeID = setInterval(function () {
               index = Math.floor(Math.random() * arr.length)
               name.innerHTML = arr[index]
            }, 50)
            if (arr.length === 1) {
               start.disabled = true
               end.disabled = true
            }
         }
         end.onclick = function () {
            clearInterval(timeID)
            arr.splice(index, 1)
            console.log(arr)
         }


      </script>
   </body>

</html>